<?php
namespace App\Http\Controllers\ApiXcx;

use App\Service\ApiXcx\AddressService;
use Illuminate\Http\Request;

class AddressController extends BaseController
{
    //收货地址

    protected $fieldMsgs = [
        'accepter' => ['en' => 'accepter', 'cn' => '接收人姓名'],
        'phone' => ['en' => 'phone', 'cn' => '收件人手机号的'],
        'wx_name' => ['en' => 'wx_name', 'cn' => '微信地址'],
        'wx_door' => ['en' => 'wx_door', 'cn' => '微信门牌号'],
        'wx_map' => ['en' => 'wx_map', 'cn' => '微信坐标'],
        'type' => ['en' => 'type', 'cn' => '接收人类型'],
    ];

    public function __construct()
    {
        $this->db = new AddressService();
    }

    //列表
    public function getList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $res = $this->db->getList($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //详情
    public function getOne(Request $request)
    {
        $token = $request->post('token', '');
        $id = $request->post('id', 0);
        $uid = $this->db->addApiCount($token);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //选项
    public function getOption(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $res  = $this->db->getOption();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //添加
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $validate = $this->checkField($request, $this->fieldMsgs);
        if ($validate['code'] < 0) {
            return $this->toJson(-2, $validate['msg']);
        }
        $validate['data']['door'] = $request->post('door', '');
        $res = $this->db->add($uid, $validate['data']);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //修改
    public function modify(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '地址ID必须的');
        }
        $validate = $this->checkField($request, $this->fieldMsgs);
        if ($validate['code'] < 0) {
            return $this->toJson(-2, $validate['msg']);
        }
        $validate['data']['door'] = $request->post('door', '');
        $res = $this->db->modify($id, $validate['data']);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function setDel(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '地址ID必须的');
        }
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //设置默认
    public function setDefault(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '地址ID必须的');
        }
        $res = $this->db->setDefault($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //获取默认
    public function getDefaultOne(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        $res = $this->db->getDefaultOne($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //设置选中
    public function setChecked(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '地址ID必须的');
        }
        $res = $this->db->setChecked($uid, $id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}